package www.ld.com.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.ld.com.core.group.InsertGroup;
import www.ld.com.core.group.UpdateGroup;
import www.ld.com.domain.R;
import www.ld.com.domain.category.pojo.CategoryPO;
import www.ld.com.domain.category.query.CategoryQuery;
import www.ld.com.domain.category.vo.CategoryVO;
import www.ld.com.domain.category.vo.InCategoryVO;
import www.ld.com.domain.category.vo.OutCategoryVO;
import www.ld.com.domain.vo.PageRestVO;
import www.ld.com.domain.vo.SelectTreeVO;
import www.ld.com.service.CategoryCacheService;
import www.ld.com.service.CategoryService;
import www.ld.com.utils.PageUtils;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @Resource
    private CategoryCacheService categoryCachService;

    @GetMapping("find")
    @ApiOperation(value = "查询分类")
    public R<List<SelectTreeVO>> findAll() {
        return R.ok(categoryCachService.findAll());
    }

    @GetMapping
    @ApiOperation(value = "分页查询分类")
    @PreAuthorize("hasAuthority('pms:category:select')")
    public R<PageRestVO<CategoryVO>> pageList(CategoryQuery categoryQuery) {
        PageRestVO<CategoryVO> pageRestVO = PageUtils.copyPageVO(categoryService.pageCategoryByCategoryQuery(categoryQuery), CategoryVO.class);
        pageRestVO.getContent().forEach(v -> {
            String parentName = categoryCachService.getCategoryNameById( v.getParentId());
            v.setParentName(StrUtil.isNotBlank(parentName) ? parentName : "");
        });
        return R.ok(pageRestVO);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "获取分类详情")
    @PreAuthorize("hasAuthority('pms:category:select')")
    public R<OutCategoryVO> getCategoryById(@PathVariable(value = "id", required = true) String id) {
        CategoryPO brandPO = categoryService.getCategoryPOById(id);
        return R.ok(BeanUtil.toBean(brandPO, OutCategoryVO.class));
    }

    @ApiOperation(value = "新增分类")
    @PostMapping
    @PreAuthorize("hasAuthority('pms:category:create')")
    public R save(@RequestBody @Validated(value = {InsertGroup.class}) InCategoryVO inCategoryVO) {
        categoryService.saveCategoryPO(BeanUtil.toBean(inCategoryVO, CategoryPO.class));
        return R.ok();
    }

    @ApiOperation(value = "修改分类")
    @PutMapping
    @PreAuthorize("hasAuthority('pms:category:update')")
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) InCategoryVO inCategoryVO) {
        categoryService.updateCategoryPO(BeanUtil.toBean(inCategoryVO, CategoryPO.class));
        return R.ok();
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('pms:category:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        categoryService.deleteCategoryById(id);
        return R.ok();
    }

}
